package com.ycsj.goldmedalnewconcept.activity;

import java.io.IOException;
import java.text.DecimalFormat;
import java.util.ArrayList;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.ProgressBar;
import android.widget.RatingBar;
import android.widget.TextView;
import android.widget.Toast;

import com.google.gson.Gson;
import com.squareup.okhttp.Call;
import com.squareup.okhttp.Callback;
import com.squareup.okhttp.FormEncodingBuilder;
import com.squareup.okhttp.OkHttpClient;
import com.squareup.okhttp.Request;
import com.squareup.okhttp.Response;
import com.ycsj.goldmedalnewconcept.bean.CommentResponse;
import com.ycsj.goldmedalnewconcept.bean.CommentResponse.listDetail;
import com.ycsj.goldmedalnewconcept.constant.Constant;
import com.ycsj.goldmedalnewconcept.utils.SPUtil;
import com.ycsj.goldmedalnewconcept.R;

//评价老师(汇总)
public class ParentsEvaluationActivity extends Activity implements
		OnClickListener {

	private ListView lv_parentsevaluation;
	private String role = "";
	private ImageView iv_back;
	private ImageView iv_add;
	private TextView tv_percent5;
	private TextView tv_percent4;
	private TextView tv_percent3;
	private TextView tv_percent2;
	private TextView tv_percent1;
	private TextView tv_title;
	private ProgressBar bar5;
	private ProgressBar bar4;
	private ProgressBar bar3;
	private ProgressBar bar2;
	private ProgressBar bar1;
	private TextView tv_sumscore;
	private RatingBar ratingBar;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_parentsevaluation);
		role = SPUtil.getString(this, "role", "");// 获取账号角色类型
		teacherinfo = getIntent().getStringArrayExtra("teacherinfo");
		tEACHER_MOBILE = getIntent().getStringExtra("TEACHER_MOBILE");
		initView();
		setOnListener();
		
		if("2".equals(role)){
			tv_title.setText("评价记录");
		}
		if("3".equals(role)){
			tv_title.setText("评价记录");
		}
	}

	
	
	@Override
	protected void onResume() {
		// TODO Auto-generated method stub
		super.onResume();
		initData();
	}



	private void initView() {
		lv_parentsevaluation = (ListView) findViewById(R.id.lv_parentsevaluation);
		iv_back = (ImageView) findViewById(R.id.iv_back);
		iv_add = (ImageView) findViewById(R.id.iv_add);
		
		tv_percent5 = (TextView) findViewById(R.id.tv_percent5);
		tv_percent4 = (TextView) findViewById(R.id.tv_percent4);
		tv_percent3 = (TextView) findViewById(R.id.tv_percent3);
		tv_percent2 = (TextView) findViewById(R.id.tv_percent2);
		tv_percent1 = (TextView) findViewById(R.id.tv_percent1);
		tv_title = (TextView) findViewById(R.id.tv_title);
		
		bar5 = (ProgressBar) findViewById(R.id.bar5);
		bar4 = (ProgressBar) findViewById(R.id.bar4);
		bar3 = (ProgressBar) findViewById(R.id.bar3);
		bar2 = (ProgressBar) findViewById(R.id.bar2);
		bar1 = (ProgressBar) findViewById(R.id.bar1);
		
		tv_sumscore = (TextView) findViewById(R.id.tv_sumscore);
		ratingBar = (RatingBar) findViewById(R.id.ratingBar);
		
		
		
		
	}

	private void setOnListener() {
		iv_back.setOnClickListener(this);
		iv_add.setOnClickListener(this);
		
		if("3".equals(role)){
			iv_add.setVisibility(View.GONE);
		}else{
			lv_parentsevaluation.setOnItemClickListener(new OnItemClickListener() {

				@Override
				public void onItemClick(AdapterView<?> parent, View view,
						int position, long id) {
//					Toast.makeText(ParentsEvaluationActivity.this, "", 0).show();
					if("1".equals(role)){
						
						Intent intent=new Intent(ParentsEvaluationActivity.this,AddCommentActivity.class);
						
						intent.putExtra("type", "9");
						intent.putExtra("rowid", list.get(position).ROW_ID);
						intent.putExtra("teacherinfo", teacherinfo);
						startActivity(intent);
					}
				}
			});
		}
	}

	ArrayList<listDetail> list = new ArrayList<listDetail>();
	MyAdapter adapter = new MyAdapter();

	private void initData() {
		
		if("3".equals(role)){
//			OkHttpClient mOkHttpClient = new OkHttpClient();
//			// 创建一个Request
//			final Request request = new Request.Builder()
//			.url("http://192.168.0.198:8888/ycsj_platform/PersonalAssistantMsg?type=11&c=15098012693&role=1&teachermobile=13006314760")
//			.build();
//			// new call
//			Call call = mOkHttpClient.newCall(request);
//			call.enqueue(callBack);
			
			OkHttpClient mOkHttpClient = new OkHttpClient();
//			mOkHttpClient.setHostnameVerifier(SSLSocketFactory.ALLOW_ALL_HOSTNAME_VERIFIER);
			FormEncodingBuilder builder = new FormEncodingBuilder();
			builder.add("type", "11");
			builder.add("c", SPUtil.getString(ParentsEvaluationActivity.this, "account", ""));
			builder.add("role", SPUtil.getString(ParentsEvaluationActivity.this, "role", ""));
			builder.add("teachermobile", tEACHER_MOBILE);
			Request request = new Request.Builder()
			.url(Constant.SERVER_URL + "PersonalAssistantMsg")
			.post(builder.build()).build();
			mOkHttpClient.newCall(request).enqueue(callBack);
			iv_add.setVisibility(iv_add.GONE);
		}else if("2".equals(role)){
			OkHttpClient mOkHttpClient = new OkHttpClient();
			// 创建一个Request
			final Request request = new Request.Builder()
			.url(Constant.SERVER_URL +"PersonalAssistantMsg?type=11&c="+SPUtil.getString(this, "account", "")+"&role="+SPUtil.getString(this, "role", "")+"&teachermobile="+tEACHER_MOBILE)
			.build();
			// new call
			Call call = mOkHttpClient.newCall(request);
			call.enqueue(callBack);
			iv_add.setVisibility(iv_add.GONE);
			tv_title.setText("评价记录");
			
		}else if("1".equals(role)){
//			OkHttpClient mOkHttpClient = new OkHttpClient();
//			// 创建一个Request
//			final Request request = new Request.Builder()
//			.url("http://192.168.0.198:8888/ycsj_platform/PersonalAssistantMsg?type=10&c=15098012693&role=1")
//			.build();
//			// new call
//			Call call = mOkHttpClient.newCall(request);
//			call.enqueue(callBack);
			OkHttpClient mOkHttpClient = new OkHttpClient();
//			mOkHttpClient.setHostnameVerifier(SSLSocketFactory.ALLOW_ALL_HOSTNAME_VERIFIER);
			FormEncodingBuilder builder = new FormEncodingBuilder();
			builder.add("type", "10");
			builder.add("c", SPUtil.getString(ParentsEvaluationActivity.this, "account", ""));
			builder.add("role", SPUtil.getString(ParentsEvaluationActivity.this, "role", ""));
			builder.add("teachermobile", tEACHER_MOBILE);
			Request request = new Request.Builder()
			.url(Constant.SERVER_URL + "PersonalAssistantMsg")
			.post(builder.build()).build();
			mOkHttpClient.newCall(request).enqueue(callBack);
		}else if("2".equals(role)){
			OkHttpClient mOkHttpClient = new OkHttpClient();
//			mOkHttpClient.setHostnameVerifier(SSLSocketFactory.ALLOW_ALL_HOSTNAME_VERIFIER);
			FormEncodingBuilder builder = new FormEncodingBuilder();
			builder.add("type", "11");
			builder.add("c", SPUtil.getString(ParentsEvaluationActivity.this, "account", ""));
			builder.add("role", SPUtil.getString(ParentsEvaluationActivity.this, "role", ""));
			builder.add("teachermobile", SPUtil.getString(ParentsEvaluationActivity.this, "account", ""));
			Request request = new Request.Builder()
			.url(Constant.SERVER_URL + "PersonalAssistantMsg")
			.post(builder.build()).build();
			mOkHttpClient.newCall(request).enqueue(callBack);
			
		}
		
		

	}

	class MyAdapter extends BaseAdapter {

		@Override
		public int getCount() {
			// TODO Auto-generated method stub
			return list.size();
		}

		@Override
		public Object getItem(int position) {
			// TODO Auto-generated method stub
			return null;
		}

		@Override
		public long getItemId(int position) {
			// TODO Auto-generated method stub
			return 0;
		}

		@Override
		public View getView(int position, View convertView, ViewGroup parent) {
			// TODO Auto-generated method stub
			ViewHolder vh;
			if (convertView == null) {
				vh = new ViewHolder();
				convertView = View.inflate(parent.getContext(),
						R.layout.item_parentsevaluation, null);
				
				
				vh.tv_pe_content = (TextView) convertView
						.findViewById(R.id.tv_pe_content);
				vh.tv_classnum = (TextView) convertView
						.findViewById(R.id.tv_classnum);
				vh.tv_updatedate = (TextView) convertView
						.findViewById(R.id.tv_updatedate);
				vh.ratingBar=(RatingBar) convertView
						.findViewById(R.id.ratingBar);
				convertView.setTag(vh);
			} else {
				vh = (ViewHolder) convertView.getTag();
			}
			vh.tv_classnum.setText("班级:"+list.get(position).USER_CLASS);
			vh.tv_pe_content.setText(list.get(position).CONTENT);
			vh.tv_updatedate.setText(list.get(position).UPDATE_DATE);
			vh.ratingBar.setRating((float) list.get(position).SCORE);
			return convertView;
		}

		class ViewHolder {
			private TextView  tv_classnum;
			private TextView tv_pe_content;
			private TextView  tv_updatedate;
			private ImageView iv_grammar_second_bg;
			private RatingBar ratingBar;
		}
	}

	Handler handler = new Handler();
	Callback callBack = new Callback() {

		@Override
		public void onResponse(Response arg0) throws IOException {
			String json = arg0.body().string();
			Log.e("test", json);
			Gson gson = new Gson();
			if(!("{\"state\":\"-1\"}").equals(json)){
				
				list.removeAll(list);
			final CommentResponse fromJson = gson.fromJson(json,CommentResponse.class);
			list.addAll(fromJson.list);
			handler.postDelayed(new Runnable() {

				private float percent5;
				private float percent4;
				private float percent3;
				private float percent2;
				private float percent1;

				@Override
				public void run() {

					
					for (int i = 0; i < fromJson.data.size(); i++) {
						if( fromJson.data.get(i).SCORE==5){
							percent5 = ((fromJson.data.get(i).COUNT*fromJson.data.get(i).SCORE)/(float)fromJson.point.get(0).SUM_SCORE* 100);
						}else if(fromJson.data.get(i).SCORE==4){
							percent4 =  ((fromJson.data.get(i).COUNT*fromJson.data.get(i).SCORE)/(float)fromJson.point.get(0).SUM_SCORE* 100);
						}else if(fromJson.data.get(i).SCORE==3){
							percent3 =  ((fromJson.data.get(i).COUNT*fromJson.data.get(i).SCORE)/(float)fromJson.point.get(0).SUM_SCORE* 100);
						}else if(fromJson.data.get(i).SCORE==2){
							percent2 =  ((fromJson.data.get(i).COUNT*fromJson.data.get(i).SCORE)/(float)fromJson.point.get(0).SUM_SCORE* 100);
						}else if(fromJson.data.get(i).SCORE==1){
							percent1 =  ((fromJson.data.get(i).COUNT*fromJson.data.get(i).SCORE)/(float)fromJson.point.get(0).SUM_SCORE* 100);
						}
						
					}
					DecimalFormat df = new DecimalFormat("0.0");//格式化小数   
					tv_percent5.setText(df.format(percent5)+"%");
					bar5.setProgress((int)percent5);
					tv_percent4.setText(df.format(percent4)+"%");
					bar4.setProgress((int)percent4);
					tv_percent3.setText(df.format(percent3)+"%");
					bar3.setProgress((int)percent3);
					tv_percent2.setText(df.format(percent2)+"%");
					bar2.setProgress((int)percent2);
					tv_percent1.setText(df.format(percent1)+"%");
					bar1.setProgress((int)percent1);
					
					float sumscoreRatingBar=fromJson.point.get(0).SUM_SCORE/(float)(fromJson.point.get(0).COUNT*5);
					ratingBar.setRating(sumscoreRatingBar*5);
					String format = df.format(sumscoreRatingBar*5);
					
					tv_sumscore.setText(format+"分");
					lv_parentsevaluation.setAdapter(adapter);
				}
			}, 0);
		}
		}
		@Override
		public void onFailure(Request arg0, IOException arg1) {
			// TODO Auto-generated method stub

		}
	};
	private String[] teacherinfo;
	private String tEACHER_MOBILE;


	@Override
	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.iv_back:
			finish();
			break;
		case R.id.iv_add:
			Intent intent=new Intent(ParentsEvaluationActivity.this,AddCommentActivity.class);
			intent.putExtra("type", "8");
			intent.putExtra("rowid", "");
			startActivity(intent);
			break;
		default:
			break;
		}
	}
}
